/**
 * 单词搜索结果组件
 * @author 杨世峰
 */

<template>
  <div
    class="search-result-card"
    v-if="JSON.stringify(this.$store.getters.currentWord) != '{}'"
  >
    <el-card
      class="box-card"
      shadow="always"
    >
      <div class="word-detail-table">
        <el-row type="flex">
          <el-col :span="20">
            <h3
              class="word-spell"
              v-text="this.$store.getters.currentWord.wordSpell"
            ></h3>
          </el-col>
          <el-col :span="4">
            <el-dropdown
              split-button
              type="primary"
              trigger="click"
              @command="handleAddWordToBooklet"
            >添加到单词本<el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  v-for="booklet in bookletList"
                  :key="booklet.bookletId"
                  divided
                  :command="booklet.bookletId"
                >{{booklet.bookletName}}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <h3
              class="word-soundmark"
              v-text="this.$store.getters.currentWord.soundmark"
            ></h3>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="24">
            <div>
              <h3
                class="word-meaning"
                v-text="this.$store.getters.currentWord.paraphrase"
              ></h3>
            </div>
          </el-col>
        </el-row>

      </div>
    </el-card>

    <el-dialog
      title="填写备注信息"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-input v-model="mark"></el-input>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handleSubmit"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserBooklets, addEntryToBooklet } from "@/api/user/bookletApi.js";

export default {
  data() {
    return {
      bookletList: [],
      dialogVisible: false,
      mark: "",
      bookletId: NaN,
    };
  },
  computed: {
    userId() {
      return this.$store.getters.userId;
    },
  },

  methods: {
    handleBooklets() {
      console.log("点击弹出单词本列表");
    },

    // 获取单词本列表
    getBooklets() {
      getUserBooklets(this.userId, { page: 1, pageSize: 100 })
        .then((res) => {
          if (res.resultCode == 200) {
            this.bookletList = res.data.list;
          }
        })
        .catch();
    },

    // 处理将当前单词添加到选择的单词本中
    handleAddWordToBooklet(bookletId) {
      // 弹出一个窗口来提示填写备注信息
      this.dialogVisible = true;
      this.bookletId = bookletId;
    },

    handleSubmit() {
      addEntryToBooklet(this.bookletId, {
        wordId: this.$store.getters.currentWord.wordId,
        remark: this.mark,
      })
        .then((res) => {
          if (res.resultCode == 200) {
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
          } else {
            this.$notify({
              title: "失败",
              message: res.message,
              type: "warning",
            });
          }
        })
        .catch()
        .then(() => {
          this.bookletId = NaN;
          this.mark = "";
          this.dialogVisible = false;
        });
    },
  },
  created() {
    this.getBooklets();
    
  },
};
</script>

<style scoped>
.search-result-card {
  padding-top: 100px;
  max-width: 1000px;
  margin: 0 auto 0;
}

.word-spell {
  font-size: xxx-large;
  margin: 20px 0px -5px 10px;
}

.word-soundmark {
  margin: 20px 0px -5px 10px;
}

.word-meaning {
  font-size: inherit;
  margin: -5px 0px 10px 10px;
}
</style>